package com.handinfo.game;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import com.handinfo.engine.AbstractGameScreen;
import com.handinfo.engine.GameManager;
import com.handinfo.engine.resource.ObjectAdapter;
import com.handinfo.engine.resource.ResHead;
import com.handinfo.engine.resource.ResourceManager;
import com.handinfo.engine.resource.SerializeableImage;

public class ActorBuffer
{
	public static final byte TYPE_BUF = 0;
	public static final byte TYPE_DEBUF = 1;

	public static Image s_icon;
	public byte m_type = 0;
	// public short m_id
	public long m_guid = 0;
	public short m_iconId = 0;
	public int m_duration; // 伤害持续帧数

	public static final String BUFFER_ICON = "/img/bufficon.png";
	public static final int[] SIZE_IMG_ICON = { 8, 8 };

	public static void initStaticResource()
	{
		ResHead head = new ResHead(ObjectAdapter.RESOURCE_TYPE_PNG,
				BUFFER_ICON, 1);
		s_icon = ((SerializeableImage) ResourceManager
				.getInstance()
				.loadResource(GameManager.getInstance().m_resource_static, head)).m_image;
	}

	public static void disposeStaticResource()
	{
		if (s_icon == null)
			return;
		ResHead head = new ResHead(ObjectAdapter.RESOURCE_TYPE_PNG,
				BUFFER_ICON, 1);
		ResourceManager.getInstance().removeResource(
				GameManager.getInstance().m_resource_static, head);
		s_icon = null;
	}

	public void init(AbstractGameActor host, byte type, long id, short iconId,
			int duration)
	{
		m_type = type;
		m_guid = id;
		m_iconId = iconId;
		m_duration = duration * GameManager.FRAME_PER_SECOND;
	}

	public void draw(Graphics g, int x, int y, int anchor)
	{
		// 当持续时间低于3秒后开始闪烁
		if (m_duration > 30 || m_duration % 6 < 3) {
			if (m_iconId != -1) {
				AbstractGameScreen.drawRegion(g, s_icon, m_iconId
						* SIZE_IMG_ICON[0], 0, SIZE_IMG_ICON[0],
						SIZE_IMG_ICON[1], 0, x, y, anchor);
			}
		}
		if (m_duration > 0)
			m_duration--;
	}
}
